package com.ouc.edu.ssm;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ouc.edu.ssm.bean.User;
import com.ouc.edu.ssm.service.UserService;
import com.ouc.edu.ssm.service.impl.UserServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author: yyi
 * @date: 2018/8/21 15:35
 * @parameters:
 * @desc:
 **/
public class Junit {
    ApplicationContext context=null;
    @Before
    public void before(){
        context = new ClassPathXmlApplicationContext(
                new String[]{"classpath:spring/spring-mvc.xml","classpath:spring/spring-mybatis.xml"});

    }
    @Test
    public void test(){
        UserService userService = (UserService) context.getBean("userServiceImpl");
        List user = userService.selectList(null);
        User use = new User();
        use.setUsername("ouc");
        use.setPassword("oucb416");
        userService.insert(use);
        System.out.println(user);

    }
    @Test
    public void testPage(){
        UserService userService = (UserService) context.getBean("userServiceImpl");
        Page page = new Page(1,2);
        page =  userService.selectUserPage(page);
        System.out.println(page.getRecords());
        for (int i = 0 ;i<page.getRecords().size();i++){
            User user = (User) page.getRecords().get(i);
            System.out.println(user.getUsername());
        }
    }
    @Test
    public void testMulCondition(){
        UserService userService = (UserService) context.getBean("userServiceImpl");
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.where("username={0}","wang11").and("password={0}","xiao11");

        List list = userService.selectList(entityWrapper);
        System.out.println(list);
    }
    @Test
    public void testupdateCondition(){
        UserService userService = (UserService) context.getBean("userServiceImpl");
       User user = new User();
       //user.setId(170);
       //user.setUsername("xiaowang");
       user.setPassword("xiaohong111");
        //UPDATE user  SET `password`=? WHERE  (username=?)

       EntityWrapper entityWrapper = new EntityWrapper();
       entityWrapper.where("username={0}","xiaowang");
       userService.update(user,entityWrapper);
    }
    @Test
    public void testeqCondition(){
        UserService userService = (UserService) context.getBean("userServiceImpl");
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("username","yangyi").where("password={0}","456");
        List list = userService.selectList(entityWrapper);

    }
}
